package cs341.group2.mycontacts;

import java.util.ArrayList;
import java.util.HashMap;

import cs341.group2.mycontacts.Database.MyContactsDB;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ButtonAdapter extends BaseAdapter {
    private Context mContext;
    private MyContactsDB myContactsDb;
    private ArrayList<HashMap<String, String>> items;

    public ButtonAdapter(Context context, ArrayList<HashMap<String, String>> items) {
    	mContext = context;
    	myContactsDb = new MyContactsDB(mContext);
        this.items = items;

    }
    
    // Declare button click listener variable
    private OnClickListener mOnButtonClick;

    // Method to set button click listener variable
    public void setOnButtonClickListener(OnClickListener listener) {
        mOnButtonClick = listener;
    }

    public ButtonAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return items.size();
    }

    public Object getItem(int position) {
        return items.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
	   LayoutInflater inflater = LayoutInflater.from(parent.getContext());
	   view = inflater.inflate(R.layout.contact_list, parent, false);
       
       final HashMap<String, String> item = items.get(position);
       
       TextView textView = (TextView)view.findViewById(R.id.contactId);
       textView.setText(item.get("contactId"));
       textView.setId(Integer.parseInt(item.get("contactId")));
       
       final Button contact = (Button)view.findViewById(R.id.contactName);
       contact.setText(item.get("contactName"));
       contact.setId(Integer.parseInt(item.get("contactId")));
       
       final Button remove = (Button)view.findViewById(R.id.contactRemove);
       remove.setText(item.get("contactRemove"));
       remove.setId(Integer.parseInt(item.get("contactId")));
       
       //on click for remove from category button
       remove.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Async method call here
				myContactsDb.open();
				items.remove(position);
				notifyDataSetChanged();
				myContactsDb.RemoveContactFromCategory(remove.getId());
				myContactsDb.close();
			}
		});
       
       contact.setOnClickListener(new View.OnClickListener() {
   		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, ContactPage.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("state", "view");
				intent.putExtra("contactId", Integer.parseInt(item.get("contactId")));
				mContext.startActivity(intent);
			}
		});
       
       return view;
       
    }

}